﻿using System;
using System.Collections.Generic;
using System.Text;
using TCCHWLib.Commands;
using System.Windows.Forms;

namespace TCCCompiler
{
   public class SimpleExpression:IExpression
    {
        List<IExpression> Parts = new List<IExpression>();
        TreeNode Currennt_Node;       
        List<string> operators = new List<string>();
        bool t_for;
        string t_for_index;
        bool is_arr;

        public void Get_Expression()
        {
            for (int i = 0; i < Currennt_Node.Nodes.Count; i++)
            {
                switch (Currennt_Node.Nodes[i].Text)
                {
                    //case "EXP":
                    //    SimpleExpression exp = new SimpleExpression(Currennt_Node.Nodes[i],this.Current_Batch);
                    //    Current_Batch = exp.Current_Batch;
                    //    Parts.Add(exp);
                    //    break;

                    case "Term":
                        Term term = new Term(Currennt_Node.Nodes[i],Current_Batch,t_for,t_for_index,is_arr);
                        Current_Batch = term.Current_Batch;
                        Parts.Add(term);
                        break;

                    case "Operator":
                        operators.Add(Currennt_Node.Nodes[i].Nodes[0].Text.Trim());
                        break;
                }
            }
        }
       public void evaluate_Exp()
       {
           

           ParameterType parameter1_type;
           ParameterType parameter2_type;

          // this.Commands.AddRange(Parts[0].Commands);
           
           object parameter_1 = Parts[0].Value;
           if (Parts.Count == 1)
           {
               this.Type = Parts[0].Type;
               this.Value = parameter_1;
               return;
           }
           parameter1_type = Parts[0].Type;
           Parts.RemoveAt(0);
           object parameter_2;

           for (int i = 0; i < Parts.Count; i++)
           {
               parameter_2 = Parts[i].Value;
               parameter2_type = Parts[i].Type;
                   
               
               switch (operators[i].Trim())
               {
                   case "+":
                       AddCommand ADD = new AddCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                       Current_Batch.AddCommand(ADD);
                       parameter_1 = Current_Batch.CommandList.Count - 1;
                       parameter1_type = ParameterType.Instruction;
                       break;

                   case "-":
                       SubCommand SUB = new SubCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                       Current_Batch.AddCommand(SUB);
                       parameter_1 = this.Current_Batch.CommandList.Count - 1;
                       parameter1_type = ParameterType.Instruction;
                       break;

                   case "/":
                       DivCommand DIV = new DivCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                       Current_Batch.AddCommand(DIV);
                       parameter_1 = this.Current_Batch.CommandList.Count - 1;
                       parameter1_type = ParameterType.Instruction;
                       break;
                   case "*":
                       MulCommand MUL = new MulCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                       Current_Batch.AddCommand(MUL);
                       parameter_1 = this.Current_Batch.CommandList.Count - 1;
                       parameter1_type = ParameterType.Instruction;
                       break;
               }
           }
           this.Value = Current_Batch.CommandList.Count - 1;
           this.Type = ParameterType.Instruction;
       }
       public SimpleExpression(TreeNode Node,BatchCommand batch,bool T_for,string T_for_index,bool Is_arr)
        {
            Current_Batch = batch;
            Currennt_Node = Node;
            t_for = T_for;
            is_arr = Is_arr;
            t_for_index = T_for_index;
            Get_Expression();
            evaluate_Exp();
            
        }
    }
}

